<%--
  @author Tharanga Punchihewa
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<script type="text/javascript">
    $(document).ready(function () {
        var pls_enter_applicant_name = "<br>" + document.getElementById("pls_enter_applicant_name").value;
        var pls_enter_submit_date = "<br>" + document.getElementById("pls_enter_submit_date").value;
        var pls_enter_applicant_address = "<br>" + document.getElementById("pls_enter_applicant_address").value;
        var pls_enter_name_of_the_spouse = "<br>" + document.getElementById("pls_enter_name_of_the_spouse").value;
        var pls_enter_name_of_the_nominated_successor = "<br>" + document.getElementById("pls_enter_name_of_the_nominated_successor").value;
        var pls_enter_valid_email = "<br>" + document.getElementById("pls_enter_valid_email").value;
        var pls_enter_valid_phone_No = "<br>" + document.getElementById("pls_enter_valid_phone_No").value;
        var pls_enter_valid_NIC_No = "<br>" + document.getElementById("pls_enter_valid_NIC_No").value;
        var pls_select_gn_division = $("#pls_select_gnDivision").val();
        var applicant_age_validate = $("#applicant_age_validate").val();
        var pls_enter_the_applicant_dob = $("#pls_enter_the_applicant_dob").val();
        var pls_enter_annual_income = $("#pls_enter_annual_income").val();
        var pls_enter_valid_annual_income = $("#pls_enter_valid_annual_income").val();
        var pls_select_nationality = $("#pls_select_nationality").val();
        $("#gov-land-init-grant-application-form").validate({
            rules: {
                personId: {
                    required: true
                },
                submitDate: {
                    required: true
                },
                gnDivisionId: {
                    selectBox: "default"
                },
                dateOfBirth: {
                    required: true,
                    isDateFormated: true,
                    ageValidate: true
                },
                annualIncome: {
                    required: true,
                    number: true
                },
                personAddress: {
                    required: true
                },
                emailId: {
                    email: true
                },
                mobileId: {
                    phoneNo: true
                },
                landPhoneId: {
                    phoneNo: true
                },
                personNICNoId: {
                    NICNo: true
                },
                nationalityIdtrue: {
                    nationality: true
                },
                nationalityIdfalse: {
                    nationality: true
                }
            },
            messages: {
                personId: {
                    required: pls_enter_applicant_name
                },
                submitDate: {
                    required: pls_enter_submit_date
                },
                gnDivisionId: {
                    selectBox: pls_select_gn_division
                },
                dateOfBirth: {
                    required: pls_enter_the_applicant_dob,
                    ageValidate: applicant_age_validate
                },
                annualIncome: {
                    required: pls_enter_annual_income,
                    number: pls_enter_valid_annual_income
                },
                personAddress: {
                    required: pls_enter_applicant_address
                },
                emailId: pls_enter_valid_email,
                mobileId: pls_enter_valid_phone_No,
                landPhoneId: pls_enter_valid_phone_No,
                personNICNoId: pls_enter_valid_NIC_No,
                nationalityIdtrue: {
                    nationality: pls_select_nationality
                },
                nationalityIdfalse: {
                    nationality: pls_select_nationality
                }
            }
        });
    });
</script>
<script language="javascript">
    function addRow(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        cell1.appendChild(element1);

        var cell2 = row.insertCell(1);
        cell2.innerHTML = "Name";

        var cell3 = row.insertCell(2);
        var element2 = document.createElement("input");
        element2.type = "text";
        cell3.appendChild(element2);

        var cell4 = row.insertCell(3);
        cell4.innerHTML = "Date Of Birth";


        var cell5 = row.insertCell(4);
        var element3 = document.createElement("input");
        element3.type = "text";
        cell5.appendChild(element3);

    }

    function deleteRow(tableID) {
        try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for (var i = 0; i < rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if (null != chkbox && true == chkbox.checked) {
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }

            }
        } catch (e) {
            alert(e);
        }
    }
    $(function () {
        var mode = document.getElementById("edit_mode").value;
        if (mode == 2) {
            var fieldIds = new Array('submitDate', 'permitHolderOccupiedTheLandtrue', 'permitHolderOccupiedTheLandfalse',
                    'presentUserNameId', 'developmentStatus', 'purposeId', 'districtId',
                    'dsDivisionId', 'gnDivisionId',
                    'addChild');
            enableFields(fieldIds);
        }
    });
    function visibleDevelopmentStatus() {
        var val = document.getElementsByName("landApplication.permitHolderOccupiedTheLand")
        if (val[1].checked) {
            $('#developmentStatusTr').show();
            $('#presentUserNameTr').hide();
        } else {

            $('#developmentStatusTr').hide();
            $('#presentUserNameTr').show();
        }
    }
    function getDsDivisionByDistrict() {
        var oSelect = document.getElementById('districtId');
        //alert("DS");
        //var options1 = '<option value="0"> -- Select -- </option> ';
        var options1 = '<option value="0">' + $('#select').val() + ' </option> ';
        for (var iCount = 0; oSelect.options[iCount]; iCount++) {
            if (oSelect.options[iCount].selected == true) {
                var id = oSelect.options[iCount].value;
                $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 2},
                        function (data) {
                            var ds = data.dsDivisionList;
                            for (var i = 0; i < ds.length; i++) {
                                options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'
                            }
                            $("select#dsDivisionId").html(options1);
                            //$("select#gnDivisionId").html('<option value="0"> -- Select -- </option> ');
                            $("select#gnDivisionId").html('<option value="0">' + $('#select').val() + '</option> ');

                        });
            }
        }
    }

    function getGnDivisionByDsDivision() {
        var oSelect = document.getElementById('dsDivisionId');
        //alert("GN");
        //var options1 = '<option value="0"> -- Select -- </option> ';
        var options1 = '<option value="0">' + $('#select').val() + ' </option> ';
        for (var iCount = 0; oSelect.options[iCount]; iCount++) {
            if (oSelect.options[iCount].selected == true) {
                var id = oSelect.options[iCount].value;
                $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 3},
                        function (data) {
                            var ds = data.gnDivisionList;
                            for (var i = 0; i < ds.length; i++) {
                                options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'
                            }
                            $("select#gnDivisionId").html(options1);
                        });
            }
        }
    }

    function searchLands() {

        var methodId = 1;

        //if (alienationTypeId == 1 || alienationTypeId == 3 || alienationTypeId == 4) {
        /*if ($('#alienationMethodSetId1').is(':checked')) {
         methodId = 1;
         } else if ($('#alienationMethodSetId3').is(':checked')) {
         methodId = 3;
         } else if ($('#alienationMethodSetId4').is(':checked')) {
         methodId = 4;
         } else {
         var alienationTypeId = document.getElementById('alienationTypeId').value;
         methodId = alienationTypeId;
         }*/

        if (methodId > 0) {
            var url = '${pageContext.request.contextPath}' + '/getPermitAlienations.do';

            myWindow = window.open(url, '', 'width=980,height=500,scrollbars=yes');
            myWindow.focus();
        } else {
            alert("Please Select Alienation Method !");
        }

    }

    function searchLands() {
        var url = '${pageContext.request.contextPath}' + '/searchPermit.do';
        myWindow = window.open(url, '', 'width=980,height=500,scrollbars=yes');
        myWindow.focus();


    }

</script>

<s:if test="editMode!=0">
    <div id="land-registration-module-home-title" class="module-home-title">
        <s:include value="../alienationApplicationNavigationInit.jsp"/>
    </div>
</s:if>
<div class="form-outer">
    <h2><s:label value="%{getText('initial_land_application.label')}"/> - <s:label
            value="%{getText('land_grants.label')}"/></h2>
    <hr/>

    <s:if test="initGrantPage">

        <s:form method="POST"
                id="gov-land-init-grant-application-form"
                onsubmit="javascript:return validateFormForInputDataSyntaxErrors('gov-land-init-grant-application-form')">


            <s:if test="hasActionErrors() || hasActionMessages()">
                <div id="message-box" class="message-box">
                    <div class="action-msg"><s:actionmessage/></div>
                    <div class="action-err"><s:actionerror/></div>
                </div>
            </s:if>

            <table>
                <tr>
                    <td><s:submit value="%{getText('search_lands.label')}" onclick="searchLands()"/></td>
                    <td><s:label value="%{getText('permit_no.label')}"/></td>
                    <td><s:textfield name="permitNo"/></td>
                    <td>
                            <%--  <s:if test="#session.userGroup =='onla'">
                                  <s:submit value="%{getText('submit.label')}" action="populateOnlineGrantsApplication"/>
                              </s:if>
                              <s:else>--%>
                        <s:submit value="%{getText('submit.label')}" action="populateInitLandGrantsApplication"/>
                            <%--</s:else>--%>
                    </td>
                </tr>
            </table>
        </s:form>
    </s:if>
    <s:else>
        <s:form action="addInitLandApplication.do" method="POST" id="gov-land-init-grant-application-form"
                onsubmit="javascript:return (validateFormForInputDataSyntaxErrors('gov-land-init-grant-application-form') && resetIncome() && selectList())"
                enctype="multipart/form-data">

            <fieldset class="submit-date-fieldset-style">
                <table class="submited-date-table">
                    <tr>
                        <td><s:label value="%{getText('submit_date.label')}" cssClass="mandatory-field"/><s:label
                                value="%{getText('date_format.label')}" cssClass="date-format"/></td>
                        <td><s:textfield id="submitDate" name="landApplication.submitDate"/></td>
                    </tr>
                </table>
            </fieldset>
            <div id="tabs">
                <ul>
                    <li><s:a href="#applicant-details"><s:label
                            value="%{getText('details_of_applicant.label')}"/></s:a></li>
                        <%--<li><s:a href="#landAlienationDetails"><s:label
                        value="%{getText('details_of_land.label')}"/></s:a></li>--%>
                    <li><s:a href="#otherDetails"><s:label value="%{getText('other_details.label')}"/></s:a></li>
                </ul>

                <div class="fieldset-style" id="applicant-details">
                    <table class="width-100" id="division">
                        <s:if test="viewMode">
                            <tr>
                                <td><s:label value="%{getText('district.label')}"/></td>
                                <td><s:textfield id="districtId" name="districtName" cssClass="width-200-px"
                                                 disabled="true"/></td>
                                <td><s:label value="%{getText('ds_division.label')}"/></td>
                                <td><s:textfield id="dsDivisionId" name="dsDivisionName" cssClass="width-200-px"
                                                 disabled="true"/></td>
                            </tr>
                            <tr>
                                <td><s:label value="%{getText('gn_division.label')}"/><span
                                        class="mandatory-field"></span>
                                </td>
                                <td>
                                    <s:textfield id="gnDivisionId" name="holder.gnDivision.name"
                                                 cssClass="width-200-px"/>
                                </td>
                            </tr>
                        </s:if>
                        <s:else>
                            <tr>
                                <td><s:label value="%{getText('district.label')}"/></td>
                                <td><s:select id="districtId" name="districtId" list="districtListAll"
                                              onchange="getDsDivisionByDistrict()" headerKey="0"
                                              headerValue="%{getText('select.label')}"
                                              cssClass="width-200-px"/></td>
                                <td><s:label value="%{getText('ds_division.label')}"/></td>
                                <td><s:select id="dsDivisionId" name="dsDivisionId" list="dsDivisionList"
                                              onchange="getGnDivisionByDsDivision()" headerKey="0"
                                              headerValue="%{getText('select.label')}" cssClass="width-200-px"/></td>
                            </tr>
                            <tr>
                                <td><s:label value="%{getText('gn_division.label')}"/><span
                                        class="mandatory-field"></span>
                                </td>
                                <td>
                                    <s:select id="gnDivisionId" name="holder.gnDivision.id" list="gnDivisionList"
                                              headerKey="0" headerValue="%{getText('select.label')}"
                                              cssClass="width-200-px"/>
                                </td>
                            </tr>
                        </s:else>
                    </table>
                    <table class="width-100">
                        <s:include value="../personalDetails.jsp"/>
                    </table>
                </div>

                    <%--<div class="fieldset-style">
                        <legend><s:label value="%{getText('details_of_land.label')}"/></legend>
                            &lt;%&ndash;<table class="width-100">
                                <s:include value="../landAlienationDetails.jsp"/>
                            </table>&ndash;%&gt;
                    </div>--%>
                <div class="fieldset-style" id="otherDetails">
                    <table class="width-100">
                        <tr>
                            <td><s:label value="%{getText('is_the_permit_holder_occupied_the_land.label')}"/></td>
                            <td colspan="3">
                                    <s:radio name="landApplication.permitHolderOccupiedTheLand"
                                             id="permitHolderOccupiedTheLand"
                                             list="#@java.util.HashMap@{'true':getText('yes.label'),'false':getText('no.label')}"
                                             onchange="visibleDevelopmentStatus()"/>
                                    <%--<s:checkbox name="landApplication.permitHolderOccupiedTheLand"
                                    id="permitHolderOccupiedTheLand"
                                    onchange="visibleDevelopmentStatus()"/></td>--%>
                        </tr>
                        <tr id="developmentStatusTr" style="display:none">
                            <td><s:label value="%{getText('development_status_of_the_land.label')}"/></td>
                            <td colspan="3">
                                <s:textarea name="landApplication.developmentStatus" cssClass="width-500-px"
                                            id="developmentStatus"/>
                            </td>
                        </tr>
                        <tr id="presentUserNameTr">
                            <td><s:label value="%{getText('name_of_the_present_user.label')}"/></td>
                            <td colspan="3">
                                <s:textarea name="landApplication.presentUserName" cssClass="width-500-px"
                                            id="presentUserNameId"/>
                            </td>
                        </tr>


                        <tr>
                            <td><s:label value="%{getText('purpose_of_the_requesting_land')}"/></td>
                                <%--<td><select name="landApplication.purpose" id="purposeId">
                                    <option>- Select -</option>
                                </select></td>--%>
                            <td><s:select id="purposeId" name="landApplication.landUsage.id"
                                          list="purposeList"/></td>
                        </tr>
                            <%--<tr>
                                <td><s:label value="%{getText('name_of_the_spouse.label')}" cssClass="mandatory-field"/></td>
                                <td colspan="3"><s:textarea cssClass="width-500-px" name="landApplication.nameOfTheSpouse"
                                                            id="nameOfTheSpouse"/></td>
                            </tr>--%>
                            <%-- <tr>
                                <td><s:label value="%{getText('name_of_the_nominated_successor.label')}"
                                             cssClass="mandatory-field"/></td>
                                <td colspan="3"><s:textarea cssClass="width-500-px"
                                                            name="landApplication.nominatedSuccessorName"
                                                            id="nominatedSuccessorName"/></td>
                            </tr>--%>


                    </table>
                </div>
            </div>


            <hr/>
            <s:hidden id="edit_mode" name="editMode"/>
            <s:hidden name="alienationType" value="2"/>
            <s:hidden name="permitNo"/>
            <s:include value="initFormSubmitButton.jsp"/>
        </s:form>
        <div>
            <s:if test="alienationType ==2">
                <s:include value="../../landTree.jsp"/>
            </s:if>
        </div>
        <%--<s:include value="../approveRejectButton.jsp"/>--%>
    </s:else>

</div>

<s:hidden id="pls_enter_applicant_name" value="%{getText('pls_enter_applicant_name')}"/>
<s:hidden id="pls_enter_submit_date" value="%{getText('pls_enter_submit_date')}"/>
<s:hidden id="pls_enter_applicant_address" value="%{getText('pls_enter_applicant_address')}"/>
<s:hidden id="pls_enter_name_of_the_spouse" value="%{getText('pls_enter_name_of_the_spouse')}"/>
<s:hidden id="pls_enter_name_of_the_nominated_successor"
          value="%{getText('pls_enter_name_of_the_nominated_successor')}"/>
<s:hidden id="pls_enter_valid_email" value="%{getText('pls_enter_valid_email')}"/>
<s:hidden id="pls_enter_valid_phone_No" value="%{getText('pls_enter_valid_phone_No')}"/>
<s:hidden id="pls_enter_valid_NIC_No" value="%{getText('pls_enter_valid_NIC_No')}"/>

<s:hidden id="pls_select_nationality" value="%{getText('pls_select_nationality.label')}"/>
<s:hidden id="select" value="%{getText('select.label')}"/>

<s:hidden id="pls_select_gnDivision" value="%{getText('pls_select_gnDivision.label')}"/>
<s:hidden id="pls_enter_the_applicant_dob" value="%{getText('pls_enter_the_applicant_dob.label')}"/>
<s:hidden id="applicant_age_validate" value="%{getText('applicant_age_validate.label')}"/>
<s:hidden id="pls_enter_annual_income" value="%{getText('pls_enter_annual_income')}"/>
<s:hidden id="pls_enter_valid_annual_income" value="%{getText('pls_enter_valid_annual_income')}"/>